package com.barunsw.bwf.ems.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPUtil {
	public static String getLocalIP() throws UnknownHostException {
		return InetAddress.getLocalHost().getHostAddress();
	}
	
	public static long convertLong(String ip) {
        long intIp = 0;

        String[] keys = ip.split("\\.");

        intIp = Long.parseLong(keys[0]) << 24;
        intIp |= Long.parseLong(keys[1]) << 16;
        intIp |= Long.parseLong(keys[2]) << 8;
        intIp |= Long.parseLong(keys[3]);

        intIp = intIp & 0xFFFFFFFF;

        return intIp;
    }

    public static String convertIPAddress(long num) {
        String strIp = null;

        int ip1 = (int)( num >>> 24 );
        int ip2 = (int)( ( num >>> 16 ) & 0xFF );
        int ip3 = (int)( ( num >>> 8 ) & 0xFF );
        int ip4 = (int)( num & 0xFF );

        strIp = ip1 + "." + ip2 + "." + ip3 + "." + ip4;

        return strIp;
    }
}
